<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="工地名称" prop="siteName">
        <el-input
          v-model="queryParams.siteName"
          placeholder="请输入工地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="towerCraneNumber">
        <el-input
          v-model="queryParams.towerCraneNumber"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警时间" prop="warningTime">
        <el-date-picker clearable
          v-model="queryParams.warningTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预警时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预警类型" prop="warningType">
        <el-select
          v-model="queryParams.warningType"
          placeholder="预警类型"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in dict.type.warning_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="逾期状态" prop="overdue">
        <el-select
          v-model="queryParams.overdue"
          placeholder="逾期状态"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in dict.type.overdue"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warning:warning:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['warning:warning:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warning:warning:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['warning:warning:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工地名称" width="120" align="center" prop="siteName" />
      <el-table-column label="设备编号" width="100"  align="center" prop="towerCraneNumber" />
      <el-table-column label="预警位置" align="center" prop="warningPosition" />
      <el-table-column label="预警类型" width="120" align="center" prop="warningType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warning_type" :value="scope.row.warningType"/>
        </template>
      </el-table-column>
      <el-table-column label="预警内容" align="center" prop="warningContent" />
      <el-table-column label="预警时间" align="center" prop="warningTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warningTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警状态" align="center" prop="warningStatic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warning_static" :value="scope.row.warningStatic"/>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="提交状态" align="center" prop="updateStatic">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.update_static" :value="scope.row.updateStatic"/>
        </template>
      </el-table-column>
      <el-table-column label="逾期状态" align="center" prop="overdue">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.overdue" :value="scope.row.overdue"/>
        </template>
      </el-table-column>
      <el-table-column label="消除时间" align="center" prop="eliminTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eliminTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消除证明" width="120" align="center" prop="eliminImg">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.eliminImg">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="用户签名" width="120" align="center" prop="userSign">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.userSign">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="推送时间" align="center" prop="pushTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pushTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warning:warning:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warning:warning:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预警信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工地编号" prop="siteNumber">
          <el-input v-model="form.siteNumber" placeholder="请输入工地编号" />
        </el-form-item>
        <el-form-item label="工地名称" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入工地名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="towerCraneNumber">
          <el-input v-model="form.towerCraneNumber" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="预警位置" prop="warningPosition">
          <el-input v-model="form.warningPosition" placeholder="请输入预警位置" />
        </el-form-item>
        <el-form-item prop="warningType">
          <span slot="label">
            预警类型
          </span>
          <el-select v-model="form.warningType">
            <el-option
              v-for="dict in dict.type.warning_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预警内容">
          <editor v-model="form.warningContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="预警时间" prop="warningTime">
          <el-date-picker clearable
            v-model="form.warningTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预警状态" prop="warningStatic">
          <span slot="label">
            预警状态
          </span>
          <el-select v-model="form.warningStatic">
            <el-option
              v-for="dict in dict.type.warning_static"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <span slot="label">
            设备类型
          </span>
          <el-select v-model="form.deviceType">
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提交状态" prop="updateStatic">
           <span slot="label">
            提交状态
          </span>
          <el-select v-model="form.updateStatic">
            <el-option
              v-for="dict in dict.type.update_static"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期状态" prop="overdue">
          <span slot="label">
            逾期状态
          </span>
          <el-select v-model="form.overdue">
            <el-option
              v-for="dict in dict.type.overdue"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消除证明"  prop="eliminImg">
          <el-upload
            action="http://121.37.110.129:6612/common/upload"
            :headers="{
             'Authorization': 'Bearer ' + gainGetToken()
            }"
            list-type="picture-card"
            :on-success="handleEliminImgPreview"
            :on-remove="handleRemove">
            <img v-if="form.eliminImg" :src="form.eliminImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="消除时间" prop="eliminTime">
          <el-date-picker clearable
            v-model="form.eliminTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择消除时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消除证明"  prop="userSign">
          <el-upload
            action="http://121.37.110.129:6612/common/upload"
            :headers="{
             'Authorization': 'Bearer ' + gainGetToken()
            }"
            list-type="picture-card"
            :on-success="handleUserSignPreview"
            :on-remove="handleRemove">
            <img v-if="form.userSign" :src="form.userSign" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="推送时间" prop="pushTime">
          <el-date-picker clearable
            v-model="form.pushTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择推送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarning, getWarning, delWarning, addWarning, updateWarning } from "@/api/system/warning";
import { getToken } from '@/utils/auth'


export default {
  name: "Warn",
  dicts: ['warning_type','warning_static','device_type','update_static','overdue'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警信息表格数据
      warningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteNumber: null,
        siteName: null,
        towerCraneNumber: null,
        warningPosition: null,
        warningType: null,
        warningContent: null,
        warningTime: null,
        warningStatic: null,
        deviceType: null,
        updateStatic: null,
        overdue: null,
        eliminImg: null,
        eliminTime: null,
        userSign: null,
        pushTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    gainGetToken(){
      return getToken()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    //消除证明
    handleEliminImgPreview(file) {
      this.form.eliminImg = file.url;
      this.dialogVisible = true;
    },
    //用户签名
    handleUserSignPreview(file){
      this.form.userSign = file.url;
      this.dialogVisible = true;
    },
    /** 查询预警信息列表 */
    getList() {
      this.loading = true;
      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteNumber: null,
        siteName: null,
        towerCraneNumber: null,
        warningPosition: null,
        warningType: null,
        warningContent: null,
        warningTime: null,
        warningStatic: null,
        deviceType: null,
        updateStatic: null,
        overdue: null,
        eliminImg: null,
        eliminTime: null,
        userSign: null,
        pushTime: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预警信息编号为"' + ids + '"的数据项？').then(function() {
        return delWarning(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('war/warning/export', {
        ...this.queryParams
      }, `warning_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
  border-radius: 6px;
}
</style>
